import lombok.Data;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Data
public class Book {
    private Integer id;
    private String title;
    private String date;
    private String author;
    private Double price;
    private int quantity;
    public Book(Integer id, String title, String date, String author, Double price, int quantity) {
        this.id = id;
        this.title = title;
        this.date = date;
        this.author = author;
        this.price = price;
        this.quantity = quantity;
    }
    public Book() {}

    @Override
    public String toString() {
        return "Books{" +
                "编号=" + id +
                ", 书名《" + title + "》" +'\'' +
                ", 出版日期=" + date +
                ", 作者='" + author + '\'' +
                ", 价格=" + price +
                ", 数量=" + quantity +
                '}';
    }

    public static class BookHandler implements Handler<Book>{

        @Override
        public List<Book> handle(ResultSet set) throws SQLException {
            List<Book> books = new ArrayList<>();
            while (set.next()) {
                // set 代表一行数据
                Book book = new Book();
                book.setId(set.getInt("id"));
                book.setTitle(set.getString("book_title"));
                book.setDate(set.getString("publication_date"));
                book.setAuthor(set.getString("author"));
                book.setPrice(set.getDouble("price"));
                book.setQuantity(set.getInt("quantity"));
                books.add(book);
            }
            return books;
        }
    }
}
